/**
 * 
 */
package com.dhcc.DRGsHosp.application.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.XmlStructureApplication;
import com.dhcc.DRGsHosp.core.domain.XmlStructure;
import com.dhcc.DRGsHosp.infra.util.StringUtils;

/**
 * 
 * @title		:XmlStructureApplicationImpl.java
 * @description	:
 * @author		:mazhaoyan
 * @date		:2017年4月18日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014
 */
@Named("xmlStructureApplicationImplqiaoyilong20181026")
@Transactional
public class XmlStructureApplicationImpl implements XmlStructureApplication {
	@Autowired
	private JdbcTemplate jdbcTemplateDrgs;//数据源 用户名 DRGS
	private QueryChannelService queryChannel;
    
	private QueryChannelService getQueryChannelService(){
	       if(queryChannel==null){
	          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel_DRGs");
	       }
	     return queryChannel;
	}
	@Override
	public List<Map<String,Object>> getTableStructure(String tableName) {
		List<Map<String,Object>> list =new LinkedList<>();
		StringBuilder sql = new StringBuilder();
		sql.append(" select tc.COLUMN_NAME,cc.COMMENTS,tc.DATA_TYPE,tc.DATA_SCALE from sys.user_tab_columns tc left join sys.all_col_comments cc  \n ");
		sql.append(" on tc.TABLE_NAME = cc.TABLE_NAME and tc.COLUMN_NAME = cc.COLUMN_NAME \n ");
		sql.append(" where tc.TABLE_NAME = '"+tableName+"' order by tc.COLUMN_ID \n ");
		list=jdbcTemplateDrgs.queryForList(sql.toString());
		return list;
	}
	
	/**
	 * 
	 * <p>Title: creatXmlStructure</p>   
	 * <p>Description: 创建节点</p>   
	 * @param xmlStructure   
	 * @see com.dhcc.DRGsHosp.application.XmlStructureApplication#creatXmlStructure(com.dhcc.DRGsHosp.core.domain.XmlStructure)
	 */
	@Override
	public void creatXmlStructure(XmlStructure xmlStructure) {
		xmlStructure.save();
    }
	
	/**
	 * 
	 * <p>Title: nodeInfoRemove</p>   
	 * <p>Description:删除节点信息 </p>   
	 * @param treeId   
	 * @see com.dhcc.DRGsHosp.application.XmlStructureApplication#nodeInfoRemove(java.lang.String)
	 */
	public void nodeInfoRemove(String treeId){
	    XmlStructure xmlStructure = new XmlStructure(treeId);
	    xmlStructure.remove();
	}
	
	/**
	 * 
	 * <p>Title: getNodeInfoByTreeId</p>   
	 * <p>Description: </p>   
	 * @param treeId
	 * @return   
	 * @see com.dhcc.DRGsHosp.application.XmlStructureApplication#getNodeInfoByTreeId(java.lang.String)
	 */
	public int getNodeInfoByNodeId(String nodeId){
	    String sql = "SELECT COUNT(*)  FROM DRGs.T_XMLSTRUCTURE T WHERE T.NODE_ID = '"+nodeId+"'";
	    int nodeInfoCount = Integer.parseInt(getQueryChannelService().createSqlQuery(sql.toString()).singleResult().toString());
	    return nodeInfoCount;
	    
	    
	}
	/**
	 * 
	 * <p>Title: getXmlStructureByParam</p>   
	 * <p>Description: </p>   
	 * @param templateType 模版类型 首页|医保|医嘱
	 * @param tableCode 数据库表名
	 * @param pType 诊断手术类型 主要|其他
	 * @param parentId 父节点
	 * @return   
	 * @see com.dhcc.DRGsHosp.application.XmlStructureApplication#getXmlStructureByParam(java.lang.String, java.lang.String, java.lang.String)
	 */
	public List<Object[]> getXmlStructureByParam(String templateType,String tableCode,String pType,String parentId){
        StringBuffer sql = new StringBuffer(
                "SELECT T.NODE_ID,T.NAME,T.NODE_LEVEL,T.XPATH,T.DATA_TYPE_CODE,"
                + "T.DATA_FORMAT_CODE,T.PTYPE,T.ISSAMENODE,T.PARENTID,T.TABLE_CODE,"
                + "T.TABLE_NAME,T.FIELD_DESC,T.FIELD_CODE,T.DATA_SCALE,T.MAP_POSITION,T.NP_NAME,T.PROPERTY_POSITION,T.VALUE_TYPE  "
                + "FROM DRGs.T_XMLSTRUCTURE T WHERE 1=1 ");
        sql.append(" AND T.TEMPLATETYPE = '"+templateType+"' ");
        if (StringUtils.hasLength(tableCode)) {
            sql.append(" AND T.TABLE_CODE = '"+tableCode+"' ");
        }
        if (StringUtils.hasLength(pType)) {
            sql.append(" AND T.PTYPE = '"+pType+"' ");
        }
        if (StringUtils.hasLength(parentId)) {
            sql.append(" AND T.PARENTID = '"+parentId+"' ");
        }
        List<Object[]> list = getQueryChannelService().createSqlQuery(sql.toString()).list();
        return list;
    }
	
	
	/**  
	 *  @Description：
	 *  @Author mazhaoyan  
	 *  @see com.dhcc.DRGsHosp.application.XmlStructureApplication#getXmlStructureByXpath(java.lang.String, java.lang.String, java.lang.String)  
	 */
	@Override
	public List<Object[]> getXmlStructureByXpath(String tableCode,String xpath) {
		StringBuffer sql = new StringBuffer(
                "SELECT T.NODE_ID,T.NAME,T.NODE_LEVEL,T.XPATH,T.DATA_TYPE_CODE,"
                + "T.DATA_FORMAT_CODE,T.PTYPE,T.ISSAMENODE,T.PARENTID,T.TABLE_CODE,"
                + "T.TABLE_NAME,T.FIELD_DESC,T.FIELD_CODE,T.DATA_SCALE,T.MAP_POSITION,T.NP_NAME,T.PROPERTY_POSITION,T.VALUE_TYPE  "
                + "FROM DRGs.T_XMLSTRUCTURE T WHERE 1=1 ");
        if (StringUtils.hasLength(tableCode)) {
            sql.append(" AND T.TABLE_CODE = '"+tableCode+"' ");
        }
        if (StringUtils.hasLength(xpath)) {
            sql.append(" AND INSTR(T.XPATH,'"+xpath+"')=1 ");
        }
        List<Object[]> list = getQueryChannelService().createSqlQuery(sql.toString()).list();
        return list;
	}
	/**
	 * 
	 * TODO 获取xml配置表信息
	 * @author ShaoZhiYuan  
	 * @see com.dhcc.DRGsHosp.application.XmlStructureApplication#findXMLTable()
	 */
	@Override
	public List<Object[]> findXMLTable(String templateType) {
		StringBuffer sql=new StringBuffer();
	    sql.append(" select xst.name, xst.node_level, xst.data_type_code, xst.data_format_code, \n");
		sql.append(" xst.table_code, xst.table_name, xst.field_code, xst.field_desc ,xst.node_id, xst.parentid, xst.ptype \n");
		sql.append(" from drgs.T_XMLSTRUCTURE xst  where xst.templateType = '"+templateType+"' order by xst.node_level ,xst.name\n");
		List<Object[]> list=getQueryChannelService().createSqlQuery(sql.toString()).list();
		return list;
	}
	/**  
	 *  @Description：
	 *  @Author mazhaoyan  
	 *  @see com.dhcc.DRGsHosp.application.XmlStructureApplication#getXmlStructureOrderByLD(java.lang.String, java.lang.String)  
	 */
	@Override
	public List<Object[]> getXmlStructureOrderByLD(String templateType, String tableCode) {
		StringBuffer sql = new StringBuffer(
                "SELECT T.NODE_ID,T.NAME,T.NODE_LEVEL,T.XPATH,T.DATA_TYPE_CODE,"
                + "T.DATA_FORMAT_CODE,T.PTYPE,T.ISSAMENODE,T.PARENTID,T.TABLE_CODE,"
                + "T.TABLE_NAME,T.FIELD_DESC,T.FIELD_CODE,T.DATA_SCALE,T.MAP_POSITION,T.NP_NAME,T.PROPERTY_POSITION,T.VALUE_TYPE  "
                + "FROM DRGs.T_XMLSTRUCTURE T WHERE 1=1 ");
		sql.append(" AND T.TABLE_CODE = '"+tableCode+"' ");
        sql.append(" AND T.TEMPLATETYPE = '"+templateType+"' ");
        sql.append(" ORDER BY T.NODE_LEVEL");
        List<Object[]> list = getQueryChannelService().createSqlQuery(sql.toString()).list();
        return list;
	}
	@Override
	public List<Map<String, Object>> findNodeConfigInfoByPath(String pathCode) {
		StringBuffer sql = new StringBuffer(" select * from drgs.T_XMLSTRUCTURE t where t.node_id = '"+pathCode+"' \n");
		return jdbcTemplateDrgs.queryForList(sql.toString()) ;
	}
	
	
}
